﻿using System;
using Gp.Scripts.Core;
using Helpers;
using QFramework;
using UnityEngine;

namespace Gp.Scripts.GameLogic {

    public class PlayerEnterAreaEvent {
        public GameArea Area;
        public PlayerEnterAreaEvent(GameArea area) {
            Area = area;
        }
    }

    public class PlayerExitAreaEvent {
        public GameArea Area;
        public PlayerExitAreaEvent(GameArea area) {
            Area = area;
        }
    }
    
    
    
    [RequireComponent(typeof(BoxCollider))]
    public class GameArea : MonoBehaviour, ICanSendEvent {
        [SerializeField] private BoxCollider boxArea;
        [Range(0, 1)] public float leftEdge = .2f;
        [Range(0, 1)] public float rightEdge = .2f;

        [SerializeField] private Renderer[] hideOnEnter;

        private void Start() {
            boxArea = GetComponent<BoxCollider>();
        }



        public Vector2 GetBorder() {
            var bounds = boxArea.bounds;
            var res = new Vector2(bounds.min.x, bounds.max.x);
            
            float len = res.y - res.x;

            res.x += len * leftEdge;
            res.y -= len * rightEdge;
            
            return res;
        }


        public void OnTriggerEnter(Collider other) {
            if (!ObjectHelper.IsPlayer(other.gameObject)) {
                return;
            }
            
            foreach (var o in hideOnEnter) {
                if (o) o.enabled = false;
            }
            this.SendEvent(new PlayerEnterAreaEvent(this));
        }


        public void OnTriggerExit(Collider other) {
            if (!ObjectHelper.IsPlayer(other.gameObject)) {
                return;
            }
            
            foreach (var o in hideOnEnter) {
                if (o) o.enabled = true;
            }
            this.SendEvent(new PlayerExitAreaEvent(this));
        }
        
        

        public IArchitecture GetArchitecture() {
            return MainArchitecture.Interface;
        }
    }
}